package com.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manage.common.result.Result;
import com.manage.dao.CmLawyerMapper;
import com.manage.pojo.entity.CmSysUser;
import com.manage.service.LawyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class LawyerServiceImpl implements LawyerService {

    @Autowired
    private CmLawyerMapper lawyerMapper;
//
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Result<List<CmSysUser>> getLawyerList(int pageNum, int pageSize) {
        IPage<CmSysUser> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CmSysUser> wrapper = new LambdaQueryWrapper<>();
        IPage<CmSysUser> cmSysUserIPage = lawyerMapper.selectPage(page, wrapper);
        List<CmSysUser> lawyerList = cmSysUserIPage.getRecords();
        if (lawyerList.isEmpty()) {
            return Result.fail(Result.NOT_FOUND, "律师列表为空");
        }
        return Result.success(lawyerList);
    }

    @Override
    public Result<CmSysUser> getLawyerDetail(Long id) {
        CmSysUser lawyer = lawyerMapper.selectLawyerById(id);
        if (lawyer == null) {
            return Result.fail(Result.NOT_FOUND, "律师不存在");
        }
        return Result.success(lawyer);
    }

    @Override
    public Result<String> addLawyer(CmSysUser lawyer) {
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        lawyer.setSalt(salt);
        // 加密密码
        lawyer.setPassword(passwordEncoder.encode(lawyer.getPassword()));
        lawyer.setCreateTime(new Date());
        lawyer.setUpdateTime(new Date());

        int result = lawyerMapper.insert(lawyer);
        if (result > 0) {
            return new Result<>(Result.SUCCESS, "添加成功",null);
        } else {
            return Result.fail(Result.SYSTEM_ERROR, "添加失败");
        }
    }

    @Override
    public Result<String> updateLawyer(CmSysUser lawyer) {
        try {

            lawyer.setUpdateTime(new Date());

            int result = lawyerMapper.updateById(lawyer);

            if (result > 0) {
                return new Result<>(Result.SUCCESS, "更新成功", null);
            } else {
                return new Result<>(Result.NOT_FOUND, "律师不存在", null);
            }
        } catch (Exception e) {
            return new Result<>(Result.SYSTEM_ERROR, "更新失败: " + e.getMessage(), null);
        }
    }



//    @Override
//    public Result<String> changeLawyerStatus(Long id, Integer status) {
//        int result = lawyerMapper.updateLawyerStatus(id, status);
//        if (result > 0) {
//            return new Result<>(Result.SUCCESS, status == 500 ? "启用成功" : "禁用成功",null);
//        } else {
//            return new Result<>(Result.SYSTEM_ERROR, "状态修改失败",null);
//        }
//    }

    @Override
    public Result<String> resetLawyerPassword(Long id ,String newPassword) {

        String encryptedPassword = passwordEncoder.encode(newPassword);
        int result = lawyerMapper.resetLawyerPassword(id, encryptedPassword);
        if (result > 0) {
            return new Result<>(Result.SUCCESS, "密码重置成功",null);
        } else {
            return new Result<>(Result.SYSTEM_ERROR, "密码重置失败",null);
        }
    }
    @Override
    public Result<String> deleteLawyer(Long id) {
        try {

            int result = lawyerMapper.deleteById(id);

            if (result > 0) {
                return new Result<>(Result.SUCCESS, "删除成功", null);
            } else {
                return new Result<>(Result.NOT_FOUND, "律师不存在", null);
            }
        } catch (Exception e) {
            return new Result<>(Result.SYSTEM_ERROR, "删除失败: " + e.getMessage(), null);
        }
    }
}